package com.sip.stream.utils.zfy.osd;

import android.graphics.Bitmap;
import com.gosuncn.zfyluancher.ZFYLauncherJNI;
import com.sip.stream.utils.zfy.config.ZFYOSDConfig;

import java.util.Date;
import java.util.Iterator;

public class BitmapOSDTool extends AbstractBaseOSDTool {
    private boolean isOsdInvert;

    public BitmapOSDTool(ZFYOSDConfig zFYOSDConfig) {
        this.isOsdInvert = false;
        readConfigs(zFYOSDConfig);
        this.isOsdInvert = zFYOSDConfig.isOsdInvert();
    }

    public boolean addOSDDynamic(Bitmap bitmap) {
        int scale = getScale(bitmap.getWidth());
        this.cacheScale = scale;
        this.cacheSrcWidth = bitmap.getWidth();
        this.cacheSrcHeight = bitmap.getHeight();
        readConfigs(this.odsConfig);
        Iterator<OSDBean> it = this.staticOSDBeans.iterator();
        while (it.hasNext()) {
            OSDBean next = it.next();
            writeOSDBean(next, bitmap, scale, next.getStartX(), next.getStartY());
        }
        Iterator<OSDBean> it2 = this.dynamicOSDBeans.iterator();
        while (it2.hasNext()) {
            OSDBean next2 = it2.next();
            int dynamicOsdType = next2.getDynamicOsdType();
            if (dynamicOsdType == 1) {
                writeASCII(getGPSStr(), bitmap, scale, next2);
            } else if (dynamicOsdType == 2) {
                writeASCII(getTimeStampStr(), bitmap, scale, next2);
            } else if (dynamicOsdType == 3) {
                writeASCII(getDeviceStatusStr(), bitmap, scale, next2);
            }
        }
        return true;
    }

    private void addOSDToRGBASCII(int[] iArr, int i, String str, int i2) {
        if (this.isOsdInvert) {
            ZFYLauncherJNI.addOSDToRGBASCIIInvert(iArr, i, str.getBytes(), str.getBytes().length, i2);
        } else {
            ZFYLauncherJNI.addOSDToRGBASCII(iArr, i, str.getBytes(), str.getBytes().length, i2);
        }
    }

    private void addOSDToRGBHZK(int[] iArr, int i, OSDBean oSDBean, int i2) {
        if (this.isOsdInvert) {
            ZFYLauncherJNI.addOSDToRGBHZKInvert(iArr, i, oSDBean.getOsdData(), oSDBean.getOsdwidth(), i2);
        } else {
            ZFYLauncherJNI.addOSDToRGBHZK(iArr, i, oSDBean.getOsdData(), oSDBean.getOsdwidth(), i2);
        }
    }

    private boolean writeASCII(String str, Bitmap bitmap, int i, OSDBean oSDBean) {
        int length = str.getBytes().length * 8 * i;
        int i2 = i * 16;
        int[] iArr = new int[length * i2];
        int startX = oSDBean.getStartX();
        if (startX < 0) {
            startX = (bitmap.getWidth() - 15) - length;
        }
        int i3 = startX;
        int startY = oSDBean.getStartY();
        bitmap.getPixels(iArr, 0, length, i3, startY, length, i2);
        addOSDToRGBASCII(iArr, length, str, i);
        bitmap.setPixels(iArr, 0, length, i3, startY, length, i2);
        return true;
    }

    public boolean writeTimeStamp(Bitmap bitmap, int i) {
        String format = this.dateFormat.format(new Date());
        int length = format.getBytes().length * 8 * i;
        int i2 = i * 16;
        int[] iArr = new int[length * i2];
        int width = (bitmap.getWidth() - 15) - length;
        bitmap.getPixels(iArr, 0, length, width, 15, length, i2);
        addOSDToRGBASCII(iArr, length, format, i);
        bitmap.setPixels(iArr, 0, length, width, 15, length, i2);
        return true;
    }

    private boolean writeOSDBean(OSDBean oSDBean, Bitmap bitmap, int i, int i2, int i3) {
        int osdwidth = oSDBean.getOsdwidth() * 8 * i;
        int i4 = i * 16;
        int[] iArr = new int[osdwidth * i4];
        bitmap.getPixels(iArr, 0, osdwidth, i2, i3, osdwidth, i4);
        addOSDToRGBHZK(iArr, osdwidth, oSDBean, i);
        bitmap.setPixels(iArr, 0, osdwidth, i2, i3, osdwidth, i4);
        return true;
    }
}
